module cl {
    export class AttackDamage {
        public damageAttack : number = 0;
        public damageDefend : number = 0;
    }

    export class JudgeMath {
        //攻击伤害核算
        //防御指数(x)将映射到函数空间  f 得出敌军攻击衰益率
        //1级衰益率90%，2级衰益率80%，...，7级衰益率30%。
        //8级可供满编旅防御 strength = 64；2级可供满编团防御 strength = 16；否则按比例减少
        public static getDefendRate(defendIndex : number, strength : number) : number{
            strength = strength || 0;

            let defendRate = 1;
            let k = 0.125;
            let strength_k = strength * k;
            if (strength_k <= defendIndex)
                defendRate = 1 - 0.1 * defendIndex;
            else
                defendRate = 1 - 0.1 * defendIndex * defendIndex / strength_k;
            return defendRate;
        }

        public static attackDamage(troopAttack : TroopEx, troopDefend : TroopEx, isCounterattack : boolean = true) : AttackDamage{
            let damage  = new AttackDamage();

            let defendIndex = Campaign.zone.defend_index[troopDefend.tid];
            let man = troopDefend.strength * troopDefend.man_strength;
            let defendRate = JudgeMath.getDefendRate(defendIndex, man);

            let attackType = troopAttack.troop_type;
            let defendType = troopDefend.troop_type;

            damage.damageDefend = troopAttack.strength
                * troopAttack.level
                * (0.2 + 0.8 * troopAttack.morale)
                * troopAttack.attack_efficiency[defendType]
                * defendRate;

            if (isCounterattack){
                damage.damageAttack = troopDefend.strength
                    * troopDefend.level
                    * (0.2 + 0.8 * troopDefend.morale)
                    * troopDefend.attack_efficiency[attackType];
            } else {
                damage.damageAttack = 0;
            }

            return damage;
        };
    }
}